package Servlets;

import java.io.IOException;
import java.util.List;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.rpc.ServiceException;

import wbs.FacadeServicesBean;
import wbs.FacadeServicesBeanService;
import wbs.FacadeServicesBeanServiceLocator;

import BusinessDelegate.FacadeBusinessDelegate;
import EntitiesDTO.CotizacionDTO;
import EntitiesDTO.ItemCotizacionDTO;
import Parser.WriterXML;
import Parser.ReaderXML;

/**
 * Servlet implementation class for Servlet: ProcesarSolicitudCotizacion
 * 
 */
public class ProcesarSolicitudCotizacion extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ProcesarSolicitudCotizacion() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession(true);

		List<ItemCotizacionDTO> itemCotizacion = (List<ItemCotizacionDTO>) session
				.getAttribute("itemscotizacion");

		String resultado = WriterXML.getInstance().SolicitarCotizacion(
				itemCotizacion);

		FacadeServicesBeanServiceLocator locator = new FacadeServicesBeanServiceLocator();
		try {

			String cotizacion = null; // lo que devuelve el WS
			FacadeServicesBean serv = locator.getFacadeServicesBeanPort();
			cotizacion = serv.obtenerCotizacion(resultado);
			CotizacionDTO cotiz = ReaderXML.getInstance().getCotizacionDTO(
					cotizacion);

			// CotizacionDTO cotiz =
			// ReaderXML.getInstance().getCotizacionDTO(new
			// File("C:\\Cotizacion.xml"));

			session.setAttribute("cotizacion", cotiz);
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("./MostrarCotizacion.jsp");

			try {
				dispatcher.forward(request, response);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				FacadeBusinessDelegate.getInstance().saveCotizacion(cotiz);
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ServiceException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// GuiaTelefonicaServerServiceLocator gsl= new
		// GuiaTelefonicaServerServiceLocator();
		// GuiaTelefonicaServer gs = gsl.getGuiaTelefonicaServerPort();
		// gs.escribirMensaje("Mensaje");
		// System.out.println("envio mensaje");

		// mandarlo al WS

	}
}